package com.huizluo.module4.chatroom;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static Integer id = 1;
    private String addr;
    private int port;

    public Server(String addr, int port) {
        this.addr = addr;
        this.port = port;
    }


    void start(){
        ServerSocket ss = null;
        Room room = new Room();

        try{
            ss = new ServerSocket(port);
            System.out.println("waiting client connect...");

            while (true){
                Socket s = ss.accept();
                User user = new User(Server.id,"",s);
                room.addUser(user);
                System.out.println("client connected "+ user.getId());
                ServerThead st =  new ServerThead(user,room);
                st.start();
                Server.id++;
            }
        }catch (IOException e){
            e.printStackTrace();
        } finally {

            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
